package com.wxg.utils;

import java.util.LinkedHashMap;

/**
 * @ClassName Order
 * @Description TODO
 * @Author Wxg
 * @Date 2018/11/4 2:14
 * @Version 1.0
 */
public class Order {

    public static final String QUOTATION_MARKS = "\"";

    private LinkedHashMap<String, String> store;

    public Order() {
        this.store = new LinkedHashMap<>();
    }

    public Order add(String key, String val){
        store.put(key, val);
        return this;
    }

    public String str() {
        if (store != null && store.size() > 0) {
            StringBuilder sbb = new StringBuilder();
            sbb.append("{");

            int i = 1, len = store.keySet().size();
            for (String key : store.keySet()) {
                sbb.append(QUOTATION_MARKS);
                sbb.append(key);
                sbb.append(QUOTATION_MARKS);
                sbb.append(":");
                sbb.append(QUOTATION_MARKS);
                sbb.append(store.get(key));
                sbb.append(QUOTATION_MARKS);
                if (i != len) {
                    sbb.append(",");
                }
                i++;
            }

            sbb.append("}");
            return sbb.toString();
        }else {
            return "{}";
        }
    }

    public static void main(String[] args) {
        Order order = new Order();
        String str = order.add("key", "${key}")
                .add("hash", "${etag}")
                .add("bucket", "${bucket}")
                .add("width", "${imageInfo.width}")
                .add("height", "${imageInfo.height}")
                .str();
        System.out.println(str);
    }
}
